<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:40
 */

namespace addons\admin\controller;


use app\common\model\AfterSale;
use app\common\model\Sellers;
use app\common\model\Trade;
use app\common\model\TradeOrder;
use app\common\model\Users;
use app\common\OrderStatus;

class Order extends Common
{
    /**
     * 后台管理员-订单列表
     * @return mixed
     */
    public function list_()
    {
        $this->check_login();
        #获取订单状态
        $order_status = OrderStatus::get_order_status();
        $this->assign("order_status", $order_status);
        $this->assign("back_orders", OrderStatus::DAIFAHUO);
        #订单列表数据
        $param = input();
        $this->assign("param", $param);
        $list_ = \app\common\model\Order::list_($param);
        $this->assign("list_", $list_);
        if (input('export')) {
            goto EXPORT;
        }
        return $this->fetch();
        EXPORT:
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=data.xls");
        return $this->fetch("export_admin_order");
    }

    /**
     * 订单详情
     * @return mixed
     */
    public function order_details()
    {
        $this->check_login();
        $param = input();
        $order_data = \app\common\model\Order::get_one_order_($param['order_id']);  #获取订单信息
        $details_ = \app\common\model\Order::order_details($param['order_id']);     #获取子订单信息
        $relation_ = TradeOrder::get_trade_by_order_id($param['order_id']);
        $trade = Trade::get_trade_by_code(isset($relation_->code) ? $relation_->code : 0);
        $this->assign("order_data", $order_data);
        $this->assign("order_detail", $details_);
        $this->assign("trade", $trade);
        return $this->fetch();
    }

    /**
     * 订单售后列表
     */
    public function customer_service()
    {
        $param = input();
        $list_ = AfterSale::list_($param);
        $this->assign("param", $param);
        $this->assign("list_", $list_);
        return $this->fetch();
    }

    /**
     * 订单售后列表
     */
    public function customer_service_handle()
    {
        $order_id = input("order_id");
        $after_sale = AfterSale::get(["order_id" => $order_id]);
        $order = \app\common\model\Order::get(["order_id" => $order_id]);
        if (empty($after_sale)) {
            $this->error("未找到售后详情");
        }
        if (empty($order)) {
            $this->error("未找到售后关联的订单");
        }
        $submit = input("submit");
        $result = input("result");
        $change_value = input("change_value");
        $examine_complate = input("examine_complate");
        if ($submit == 2) { // 保存备注
            $after_sale->result = $result;
            $after_sale->save();
            $this->success("提交成功");
        }
        if ($examine_complate == 2) { // 修改售后状态
            $after_sale->status = 1;
            $after_sale->save();
            $this->success("提交成功");
        }
        if ($change_value > 0) {  // 金额变动
            $member = Users::get(["id" => $after_sale->user_id]);
            $seller = Sellers::get(["id" => $after_sale->seller_id]);
            $seller = Users::get(["id"=> $seller->user_id]);
            Trade::customer_service_change($member, $change_value, "订单{$order_id}售后返回余额");
            Trade::customer_service_change($seller, (0 - $change_value), "订单{$order_id}售后扣除您的余额");
            $after_sale->status = 1;
            $after_sale->save();
            $this->success("提交成功");
        }
        $this->assign("after_sale", $after_sale);
        $this->assign("order", $order);
        return $this->fetch();
    }


}